package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.EnterpriseAppConfig;
import com.xhwl.common.pojo.cent.sdata.Industry;
import com.xhwl.data.dao.IEnterpriseAppConfigDao;
import com.xhwl.data.service.IEnterpriseAppConfigService;
import com.xhwl.data.service.IIndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 移动端产品配置
 *
 * @Author: lrp
 * @Description: 移动端产品配置
 * @Date: Create in 10:05 2022/05/30
 * @menu 移动端产品配置
 */
@Service
public class EnterpriseAppConfigServiceImpl extends ServiceImpl<IEnterpriseAppConfigDao, EnterpriseAppConfig> implements IEnterpriseAppConfigService {

    @Autowired
    private IEnterpriseAppConfigDao enterpriseAppConfigDao;

    @Autowired
    private IEnterpriseAppConfigService enterpriseAppConfigService;

    @Autowired
    private IIndustryService industryService;

    @Override
    public Page page(Short industryId, Page page) {
        QueryWrapper<EnterpriseAppConfig> entAppConfQueryWrapper = new QueryWrapper<>();
        entAppConfQueryWrapper.eq("industry_id", industryId);
        entAppConfQueryWrapper.isNull("enterprise_id");
        return enterpriseAppConfigDao.selectPage(page, entAppConfQueryWrapper);
    }


    @Override
    public ResultJson batchCreate(List<EnterpriseAppConfig> enterpriseAppConfigs) {
        // 平台信息检查
        Industry industry = industryService.getById(enterpriseAppConfigs.get(0).getIndustryId());
        if (null == industry) {
            return ResultJson.fail("参数错误，平台信息不存在");
        }
        QueryWrapper<EnterpriseAppConfig> entAppConfQuery = new QueryWrapper<>();
        entAppConfQuery.eq("industry_id", enterpriseAppConfigs.get(0).getIndustryId());
        entAppConfQuery.isNull("enterprise_id");
        // 如果业态配置存在不允许再次新增
        EnterpriseAppConfig one = enterpriseAppConfigDao.selectOne(entAppConfQuery);
        if (null != one) {
            return ResultJson.fail("该业态移动产品配置已存在");
        }
        ArrayList<EnterpriseAppConfig> newEnterpriseAppConfigs = new ArrayList<>(enterpriseAppConfigs.size());
        for (EnterpriseAppConfig enterpriseAppConfig : enterpriseAppConfigs) {
            // 参数检查
            ResultJson checkResult = check(enterpriseAppConfig);
            if (!checkResult.getState()) {
                return checkResult;
            }
            enterpriseAppConfigService.save(enterpriseAppConfig);
            newEnterpriseAppConfigs.add(enterpriseAppConfig);
        }
        return ResultJson.success("操作成功", newEnterpriseAppConfigs);
    }


    @Override
    public ResultJson batchUpdate(List<EnterpriseAppConfig> enterpriseAppConfigs) {
        // 平台信息检查
        Industry industry = industryService.getById(enterpriseAppConfigs.get(0).getIndustryId());
        if (null == industry) {
            return ResultJson.fail("参数错误，平台信息不存在");
        }
        List<EnterpriseAppConfig> newEnterpriseAppConfigs = new ArrayList<>();
        List<EnterpriseAppConfig> filterList = new ArrayList<>();
        List<EnterpriseAppConfig> newList = new ArrayList<>();
        for (EnterpriseAppConfig enterpriseAppConfig : enterpriseAppConfigs) {
            if (null == enterpriseAppConfig.getId()) {
                newList.add(enterpriseAppConfig);
            } else {
                filterList.add(enterpriseAppConfig);
                newEnterpriseAppConfigs.add(enterpriseAppConfig);
            }
        }
        Map<Integer, List<EnterpriseAppConfig>> collect = filterList.stream().collect(Collectors.groupingBy(EnterpriseAppConfig::getId));
        QueryWrapper<EnterpriseAppConfig> query = new QueryWrapper<>();
        query.eq("industry_id", enterpriseAppConfigs.get(0).getIndustryId());
        query.isNull("enterprise_id");
        List<EnterpriseAppConfig> enterpriseAppConfigList = enterpriseAppConfigDao.selectList(query);
        if (!CollectionUtils.isEmpty(enterpriseAppConfigList)) {
            List<Integer> entAppConfigIds = enterpriseAppConfigList.stream().map(EnterpriseAppConfig::getId).collect(Collectors.toList());
            for (Integer entAppConfigId : entAppConfigIds) {
                if (!CollectionUtils.isEmpty(collect.get(entAppConfigId))) {
                    enterpriseAppConfigService.updateById(collect.get(entAppConfigId).get(0));
                } else {
                    enterpriseAppConfigService.removeById(entAppConfigId);
                }
            }
        }
        enterpriseAppConfigService.saveBatch(newList);
        return ResultJson.success("请求成功", enterpriseAppConfigs);
    }


    public ResultJson check(EnterpriseAppConfig enterpriseAppConfig) {
        if (null == enterpriseAppConfig.getName() || enterpriseAppConfig.getName().trim().length() == 0) {
            return ResultJson.fail("参数错误");
        }
        if (null == enterpriseAppConfig.getUrl() || enterpriseAppConfig.getUrl().trim().length() == 0) {
            return ResultJson.fail("参数错误");
        }
        return ResultJson.success("检查通过");
    }
}
